package wang.z.component;

import wang.z.JFrame.Manager;
import wang.z.JFrame.UserFace;
import wang.z.JFrame.loginFrm;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.ResultInfoData2Vector;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import static javax.swing.JTable.AUTO_RESIZE_OFF;

public class AdminBookUpdate  {
    private JPanel panel_1 = new JPanel();//表格
    private DefaultTableModel BookModel;//模型表格
    private JTable BookTable;//表格
    private Vector<Vector> AllType = new Vector<Vector>();//全部类型数据
    private Vector<Vector> bookDate = new Vector<Vector>();//书数据
    private JTextField textField_1;//编号
    private JTextField textField_2;//书名
    private JTextField textField_3;//作者
    private JTextField textField_4;//价格
    private JTextField textField_5;//出版
    private JTextField textField_6;//库存
    private JTextField textField_7;//概述
    private JComboBox comboBox_1;//类别
    private JComboBox comboBox_2;//状态
    private JPanel panel_2;
    private JPanel panel = new JPanel();
    private JTextField textField;//查询输入框
    private JButton button_1;//查询按钮、
    private Vector vc;//类型的下拉vc
    private JComboBox comboBox;
    private JFrame jf = null;

    String BookId;
    public AdminBookUpdate(JFrame jf) {
        typeDate();

        this.jf = jf;
        panel.setLayout(null);
        panel.setBorder(new TitledBorder(null,
                "书籍查询", TitledBorder.LEADING, TitledBorder.TOP,
                null, new Color(255, 0, 0)));
        panel.setBounds(20, 2, 541, 70);
        textField = new JTextField();
        textField.setColumns(10);
        textField.setBounds(152, 23, 135, 27);
        panel.add(textField);
        button_1 = new JButton("查询");

        button_1.setFont(new Font("幼圆", Font.BOLD, 16));
        button_1.setBounds(300, 20, 93, 33);
        panel.add(button_1);
        comboBox = new JComboBox();
        comboBox.setFont(new Font("幼圆", Font.BOLD, 15));
        comboBox.setBounds(23, 26, 109, 24);
        comboBox.addItem("书籍名称");
        comboBox.addItem("书籍作者");
        panel.add(comboBox);
        Map<String, String> params = new HashMap<String, String>();
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Map<String, String> params1 = new HashMap<>();
                String text = textField.getText().trim();
                params1.put("text", text);
                params1.put("type", comboBox.getSelectedItem().toString());
                likeDate(params1);
            }
        });


        panel_1.setLayout(null);
        panel_1.setBorder(new TitledBorder(null, "数据信息", TitledBorder.LEADING, TitledBorder.TOP,
                         null, new Color(255, 0, 0)));
        panel_1.setBounds(20, 70, 541, 195);
        /*做一个表头栏数据  一维数组 * */
        String[] title = {"编号", "书名", "类别", "作者", "价格", "库存", "状态"};
        Vector BookTitle = new Vector();
        for (String i : title) {
            BookTitle.add(i);
        }
        /*具体的各栏行记录 先用空的二维数组占位*/
        bookDate = new Vector<Vector>();

        BookModel = new DefaultTableModel(bookDate, BookTitle);
        BookTable = new JTable(BookModel) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        BookTable.getSelectionModel().setSelectionMode(AUTO_RESIZE_OFF);

        panel_1.setLayout(null);
        JScrollPane jscrollpane = new JScrollPane();
        jscrollpane.setBounds(20, 20, 496, 154);
        jscrollpane.setViewportView(BookTable);
        panel_1.add(jscrollpane);
        likeDate(params);
        BookTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int selectedRow = BookTable.getSelectedRow();
                String id = BookTable.getValueAt(selectedRow, 0).toString();
                String bookName = BookTable.getValueAt(selectedRow, 1).toString();
                String typeName = BookTable.getValueAt(selectedRow, 2).toString();
                String author = BookTable.getValueAt(selectedRow, 3).toString();
                String price = BookTable.getValueAt(selectedRow, 4).toString();
                String number = BookTable.getValueAt(selectedRow, 5).toString();
                String now = BookTable.getValueAt(selectedRow, 6).toString();
                textField_1.setText(id);
                textField_2.setText(bookName);
                textField_3.setText(author);
                textField_4.setText(price);
                textField_5.setText(bookDate.get(selectedRow).get(8).toString());
                textField_6.setText(number);
                textField_7.setText(bookDate.get(selectedRow).get(7).toString());
                BookId = id;

                for (int i = 0; i < vc.size();i++) {
                    if (vc.get(i).equals(typeName)){
                        comboBox_1.setSelectedIndex(i);
                    }
}
                if (now.equals("下架")) {
                    comboBox_2.setSelectedIndex(1);
                }


            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        /**
         *
         */

        panel_2 = new JPanel();
        panel_2.setBorder(new TitledBorder(null,
                "修改", TitledBorder.LEADING, TitledBorder.TOP,
                null, new Color(250, 0, 0)));
        panel_2.setBounds(20, 270, 541, 260);

        panel_2.setLayout(null);
        JLabel label = new JLabel("编号：");
        label.setFont(new Font("幼圆", Font.BOLD, 14));
        label.setBounds(58, 10, 45, 27);
        panel_2.add(label);
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(101, 10, 129, 27);
        panel_2.add(textField_1);
        JLabel label_1 = new JLabel("书名：");
        label_1.setFont(new Font("幼圆", Font.BOLD, 14));
        label_1.setBounds(294, 10, 45, 27);
        panel_2.add(label_1);
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(338, 10, 128, 27);
        panel_2.add(textField_2);
        JLabel label_2 = new JLabel("作者：");
        label_2.setFont(new Font("幼圆", Font.BOLD, 14));
        label_2.setBounds(58, 58, 45, 27);
        panel_2.add(label_2);
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(101, 58, 129, 27);
        panel_2.add(textField_3);
        JLabel label_3 = new JLabel("价格：");
        label_3.setFont(new Font("幼圆", Font.BOLD, 14));
        label_3.setBounds(58, 104, 45, 27);
        panel_2.add(label_3);
        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(101, 104, 129, 27);
        panel_2.add(textField_4);
        JLabel label_4 = new JLabel("出版：");
        label_4.setFont(new Font("幼圆", Font.BOLD, 14));
        label_4.setBounds(294, 58, 45, 27);
        panel_2.add(label_4);
        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(337, 58, 129, 27);
        panel_2.add(textField_5);
        JLabel label_5 = new JLabel("类别：");
        label_5.setFont(new Font("幼圆", Font.BOLD, 14));
        label_5.setBounds(58, 189, 45, 27);
        panel_2.add(label_5);

        vc = new Vector();
        vc.add("文学");
        vc.add("科技");
        comboBox_1 = new JComboBox(vc);
        comboBox_1.setBounds(102, 190, 128, 26);
        //获取类别

        panel_2.add(comboBox_1);
        JLabel label_6 = new JLabel("库存：");
        label_6.setFont(new Font("幼圆", Font.BOLD, 14));
        label_6.setBounds(294, 104, 45, 27);
        panel_2.add(label_6);
        textField_6 = new JTextField();
        textField_6.setColumns(10);
        textField_6.setBounds(337, 104, 129, 27);
        panel_2.add(textField_6);
        JLabel label_7 = new JLabel("描述：");
        label_7.setFont(new Font("幼圆", Font.BOLD, 14));
        label_7.setBounds(58, 152, 45, 27);
        panel_2.add(label_7);
        textField_7 = new JTextField();
        textField_7.setColumns(10);
        textField_7.setBounds(101, 152, 365, 27);
        panel_2.add(textField_7);
        JLabel label_8 = new JLabel("状态：");
        label_8.setFont(new Font("幼圆", Font.BOLD, 14));
        label_8.setBounds(294, 190, 45, 27);
        panel_2.add(label_8);
        comboBox_2 = new JComboBox();
        comboBox_2.setBounds(338, 191, 128, 26);
        comboBox_2.addItem("上架");
        comboBox_2.addItem("下架");
        panel_2.add(comboBox_2);
        JButton btnNewButton_1 = new JButton("修改");
        btnNewButton_1.setBounds(300,230,100,27);
        btnNewButton_1.setFont(new Font("幼圆", Font.BOLD, 14));
        panel_2.add(btnNewButton_1);

        btnNewButton_1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = BookTable.getSelectedRow();//如果有选中的条目，则返回条目的行号，如果没有选中，那么返回-1

                if (selectedRow == -1) {
                    JOptionPane.showMessageDialog(jf, "请选择要修改的条目！");
                    return;
                }

                //防止误操作
                int result = JOptionPane.showConfirmDialog(jf, "确认要修改选中的条目吗？", "确认修改", JOptionPane.YES_NO_OPTION);
                if (result != JOptionPane.YES_OPTION) {
                    return;
                }
                Map<String ,String> paramsIn = new HashMap<>();
                String id = textField_1.getText().trim();
                String bookName = textField_2.getText().trim();
                String author = textField_3.getText().trim();
                String publish = textField_5.getText().trim();
                String number = textField_6.getText().trim();
                String price = textField_4.getText().trim();
                String remark = textField_7.getText().trim();
                String typeId = (String) comboBox_1.getSelectedItem();
                String status = (String) comboBox_2.getSelectedItem();
                for(Vector vc: AllType){
                    if (vc.get(1).equals(typeId)){
                        typeId = vc.get(0).toString();
                        break;
                    }
                }
                if(status.equals("上架")){
                    status= "1";
                }else {
                    status = "0";
                }
                paramsIn.put("bookName",bookName);
                paramsIn.put("id",id);
                paramsIn.put("author",author);
                paramsIn.put("publish",publish);
                paramsIn.put("number",number);
                paramsIn.put("price",price);
                paramsIn.put("status",status);
                paramsIn.put("remark",remark);
                paramsIn.put("typeId",typeId);
                paramsIn.put("status",status);
                paramsIn.put("BookId",BookId);
                PostUtils.postWithParams("http://localhost:8080/book/UpdateBook", paramsIn,new SuccessListener() {
                            //请求发送成功，服务器正常响应，会执行这里
                            @Override
                            public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                                ResultInfo info = JsonUtils.parseResult(result);
                                //判断info中的flag标记
                                if (info.isFlag()) {
                                    JOptionPane.showMessageDialog(jf, info.getMessage());
                                    likeDate(params);
                                } else {
                                    JOptionPane.showMessageDialog(jf, info.getMessage());
                                    if(info.getMessage().equals("用户尚未登录")){
                                        new loginFrm().init();
                                        jf.dispose();
                                    }
                                }
                            }
                        }
                        , new FailListener() {
                            //当该请求发送失败后会执行这个方法
                            @Override
                            public void fail() {
                                JOptionPane.showMessageDialog(jf, "传入的数据有误或者网路问题，请检查");
                            }
                        }
                );
            }
        });
        jf.add(panel_1);
        jf.add(panel_2);
        jf.add(panel);
        jf.add(new JPanel());
    }


    public void likeDate(Map<String, String> params) {
        PostUtils.postWithParams("http://localhost:8080/book/getbylike", params, new SuccessListener() {
            @Override
            public void success(String result) throws IOException {
                ResultInfo info = JsonUtils.parseResult(result);
                if (info.isFlag()) {
                    //消息转换
                    Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
                    vectors = tranvector(vectors);
                    //清空tableData的数据
                    bookDate.clear();
                    for (Vector vector : vectors) {

                        vector.add(vector.get(4));
                        vector.remove(4);
                        bookDate.add(vector);
                    }
                    typeChanged();
                    //刷新表格
                    BookModel.fireTableDataChanged();
                } else {
                    JOptionPane.showMessageDialog(jf, info.getMessage());
                    if(info.getMessage().equals("用户尚未登录")){
                        new loginFrm().init();
                        jf.dispose();
                    }
                }
            }
        }, new FailListener() {
            @Override
            public void fail() {
                JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
            }
        });
    }
    public void typeDate() {
        PostUtils.post("http://localhost:8080/book_type/getAll", new SuccessListener() {
            @Override
            public void success(String result) throws IOException {
                ResultInfo info = JsonUtils.parseResult(result);
                if (info.isFlag()) {
                    //消息转换
                    Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
                    for (Vector vector : vectors) {
                        AllType.add(vector);
                    }
                } else {
                    JOptionPane.showMessageDialog(jf, info.getMessage());
                    if(info.getMessage().equals("用户尚未登录")){
                        new loginFrm().init();
                        jf.dispose();
                    }
                }
            }
        }, new FailListener() {
            @Override
            public void fail() {
                JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
            }
        });
    }
    public void typeChanged(){
        for (Vector vector : bookDate) {
            for (Vector AllType : AllType) {
                if (AllType.get(0).equals(vector.get(2))){
                    vector.set(2,AllType.get(1));
                }

            }
        }
    }
    public Vector<Vector> tranvector(Vector<Vector> vector) {
        for (Vector ve : vector) {
            int o = (int) ve.get(7);
            if (o == 1) {

                ve.set(7, "上架");
            } else {
                ve.set(7, "下架");
            }
        }
        return vector;
    }

}